<?php

/**
 * CodeLeaveType form.
 *
 * @package    Amawal Invest
 * @subpackage form
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 */
class CodeLeaveTypeForm extends BaseCodeLeaveTypeForm
{
    public function configure()
    {
        unset($this['link_employee_leave_list']);
        if(!sfContext::getInstance()->getUser()->hasCredential('super_admin')){
            unset($this['branch_id']);
        }

		$this->widgetSchema['max_period'] = new sfWidgetFormTime();
		$this->validatorSchema['max_period'] = new sfValidatorTime(array('required'=>true));
        
        $this->getValidatorSchema()->setPostValidator(
        new sfValidatorAnd(array(
        'out_fn' => new sfValidatorPropelUnique(array('model'=>'CodeLeaveType','column'=>'out_fn'))
        , 'in_fn'  => new sfValidatorPropelUnique(array('model'=>'CodeLeaveType','column'=>'in_fn'))
        )));

    }


    protected  function doSave($con = null){
        parent::doSave( $con );
        
        if( $this->getObject()->getOutOfCalc() ){
            $c = new Criteria();
            $c->add( CodeLeaveTypePeer::ID , $this->getObject()->getId(), Criteria::NOT_EQUAL) ;
            $objs = CodeLeaveTypePeer::doSelect($c);

            /*@var $obj CodeLeaveType*/
            foreach ($objs as $obj){
                $obj->setOutOfCalc(0);
                $obj->save();
            }
        }


    }
}
